#include <stdio.h>
#include "cJSON.h"
#include <string.h>
#include <curl/curl.h>
#include <stdlib.h>
/*

{
	
    "perception": {
        "inputText": {
            "text": "你好"
        }
        
      },
    "userInfo": {
        "apiKey": "dee1af6fff0d4c94a389ecc8574972c3",
        "userId": "gaomin"
    }
}*/

#define N 128

//构造json请求报文
char *robot_make_request(const char * apikey,const char * text);
char *robot_make_request(const char * apikey,const char * text)
{
	//判断输入的字符串长度不为0
	if(strlen(text)==0)
	{
		return NULL;
	}
	
	cJSON * request=cJSON_CreateObject();
	cJSON * inputText=cJSON_CreateObject();
	cJSON * perception=cJSON_CreateObject();

	cJSON_AddStringToObject(inputText,"text",text);
	cJSON_AddItemToObject(perception,"inputText",inputText);
	cJSON_AddItemToObject(request,"perception",perception);

	cJSON * userInfo=cJSON_CreateObject();
	cJSON_AddStringToObject(userInfo,"apiKey",apikey);
	cJSON_AddStringToObject(userInfo,"userId","gaomin");
	cJSON_AddItemToObject(request,"userInfo",userInfo);
	



	//将JSON数据结构转为字符串
	return cJSON_Print(request);
}

//作业:发送请求报文给图灵机器人服务器,等待服务器的响应报文
char * robot_send_request(const char *request);
char * robot_send_request(const char *request)
{

	FILE * fp=NULL;
	char * response;
	size_t resplen;
	fp=open_memstream(&response,&resplen);	
	//fp=fopen("1.txt","w");

	if(NULL==fp)
	{
		perror("open file");
		return NULL;
	}

	//1.初始化curl
	CURL * curl=curl_easy_init();
	if(NULL==curl)
	{
		perror("inint error");
		return NULL;
	}
	//2.发送到百度云的字符串需要两次URL编码
	//3.
	//char * temp=curl_easy_escape(curl,requ);

	curl_easy_setopt(curl,CURLOPT_URL,"http://openapi.tuling123.com/openapi/api/v2");
	curl_easy_setopt(curl,CURLOPT_POST,1);
	curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request);
	curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
	CURLcode error=curl_easy_perform(curl);

	if(error!=CURLE_OK)
	{
		perror("perform error\n");
		curl_easy_cleanup(curl);
		free(response);
		fclose(fp);
	}



	fflush(fp);

	curl_easy_cleanup(curl);
	fclose(fp);
//	free(response);

	return response;
	
}

//解析response

void robot_process_response(const char * response);
void robot_process_response(const char * response)
{

//解析json字符串
	cJSON * json=cJSON_Parse(response);
	if(NULL==json)
	{
		const char * err_pos=cJSON_GetErrorPtr();
		if(NULL!=err_pos)
		{
			fprintf(stderr,"Error before:%s\n",err_pos);
		}
		return ;
	}
	
	
	
	cJSON * intent= cJSON_GetObjectItemCaseSensitive(json, "intent");
	if(NULL==intent)
	{

		perror("intent error\n");
		cJSON_Delete(json);
		return ;
	}
	
	cJSON * code= cJSON_GetObjectItemCaseSensitive(intent, "code");
	if(code->valueint<10000)
	{
		perror("robot error\n");
		return ;
	}

	cJSON * results= cJSON_GetObjectItemCaseSensitive(json, "results");


	if(NULL==results)
	{
		perror("results error");
		return ;
	}


	
	//遍历json的值
	cJSON * result;
	cJSON_ArrayForEach(result,results)//遍历results数组
	{
		 cJSON * resultType=cJSON_GetObjectItemCaseSensitive(result, "resultType");
	
		 if(strcmp(resultType->valuestring,"text")==0)
		 {
			cJSON 	 * values=cJSON_GetObjectItemCaseSensitive(result, "values");
			cJSON 	 * text=cJSON_GetObjectItemCaseSensitive(values, "text");
			
		printf("%s\n",text->valuestring);

		 }
	
	}






	//释放json数据结构占用的内存
	cJSON_Delete(json);

}

int main(void)
{

	char line[N]={'\0'};
	
	char * apikey="dee1af6fff0d4c94a389ecc8574972c3";
	while(fgets(line,sizeof(line),stdin)!=NULL)
	{
		//构造请求报文
		char * request=robot_make_request(apikey,line);
		//将请求报文发送给图灵机器人
		//puts(request);		
		if(NULL==request)
		{
			continue;
		}
		
		char * response=robot_send_request(request);
//		printf("response2=%s\n",response);
		robot_process_response(response);
		free(response);
	}

	return 0;
}











